//
//  NearHeadCell.swift
//  YueChat
//
//  Created by IMac on 2024/10/17.
//

import UIKit

protocol NearHeadCellHeadDelegate : AnyObject {
    

    func NearHeadBlock (index:Int)
}

class NearHeadCell: UITableViewCell, UICollectionViewDelegate, UICollectionViewDataSource {
    
    var data = [DMCCUser]()

    weak var delegate:NearHeadCellHeadDelegate?
    
    lazy var layout: UICollectionViewFlowLayout = {
        
        let layout = UICollectionViewFlowLayout()
        layout.itemSize = CGSize(width: kScreenWidth/6, height: 70)
        layout.minimumLineSpacing = 0
        layout.minimumInteritemSpacing = 0
        layout.scrollDirection = .vertical
        layout.sectionInset = UIEdgeInsets(top: 10, left: 10, bottom: 10, right: 10)
        return layout
    }()
    
    lazy var collectionView: UICollectionView = {
        let collectionView = UICollectionView(frame: .zero, collectionViewLayout: layout)
        collectionView.dataSource = self
        collectionView.backgroundColor = UIColor.red
        collectionView.delegate = self
        collectionView.isUserInteractionEnabled = true
        return collectionView
    }()

    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        self.selectionStyle = .none
        self.addSubview(collectionView)
        
        collectionView.snp.makeConstraints { make in
            make.left.right.equalTo(self.contentView)
            make.top.equalTo(self)
            make.bottom.equalTo(self)
        }
        
        collectionView.register(UINib(nibName: "GroupDetailHeadImgCell", bundle: nil), forCellWithReuseIdentifier: "GroupDetailHeadImgCell")
    }
    
    func update () {
        self.collectionView.reloadData()
    }
    
    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
        
    }
    
    
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }


    
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return self.data.count
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "GroupDetailHeadImgCell", for: indexPath) as! GroupDetailHeadImgCell
//        if indexPath.row == data.count {
////            if  !isManager {
////            } else {
////                cell.img.image = UIImage(named: "f_add")
////            }
//            cell.img.image = UIImage(named: "f_add")
//            cell.nameLab.isHidden = true
//            cell.nftImg.isHidden = true
//        } else if indexPath.row == data.count + 1 ,isManager {
//            cell.img.image = UIImage(named: "f_less")
//            cell.nameLab.isHidden = true
//            cell.nftImg.isHidden = true
//        } else {
//            cell.nameLab.isHidden = false
//            
//            let user = data[indexPath.row] as! DMCCUser
//            let groupId = member.groupId
//            let memberId = member.memberId
//            
//            let user = DMCCIMService.getInstance().getUserInfo(memberId, inGroup: groupId, refresh: false)
//
//            cell.nameLab.text = user!.getAlias()
//            cell.img.sd_setImage(with: URL(string: user!.portrait), placeholderImage: SDImageDefault)
//
//            let nftStr = user!.getNft()
//            cell.nftImg.isHidden = nftStr.isEmpty
//        }

            return cell
    }
    
//    func isManager () -> Bool {
//        let str = self.info.getGroupJoinTypeWithGroupId()
//        return str == "admin"
//    }
    
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        self.delegate?.NearHeadBlock(index: indexPath.row)
    }

}

